<!-- 部门列表 -->
<template>
    <div class='department'>
        <Card>
            <p slot="title">部门列表</p>

            <div class="search-con search-con-top">

                部门：
                <Input v-model="searchData.name" style="width: 200px"></Input>
                <Button @click="search" type="primary" shape="circle" icon="ios-search"
                    style="margin-left: 10px;">搜索</Button>
                <Button @click="reset" type="success" shape="circle" icon="ios-refresh"
                    style="margin-left: 10px;">重置</Button>
                <showRule name="addAccountBtn">
                    <Button @click="showAddDrawer" type="primary" style="float: right;" shape="circle">添加部门</Button>
                </showRule>
            </div>
            <br>

            <Table :columns="columns" :data="list" border>
                <template slot-scope="{ row , index}" slot="statusSlot">
                    <!-- <i-switch :value="!row.status" @on-change="statusChange" /> -->
                    <i-switch :value="!row.status" @on-change="statusChange(row.id, row.status)">
                        <span slot="open">开</span>
                        <span slot="close">关</span>
                    </i-switch>
                </template>
                <template slot-scope="{ row , index}" slot="memberSlot">
                    <!-- <showRule name="editAccountBtn"> -->
                    <i-button type="primary" size="small" @click="edit(row)">编辑</i-button>
                    <!-- </showRule> -->
                </template>
            </Table>
            <br>
            <Page :total="total" show-sizer show-total show-elevator @on-change="changePage"
                @on-page-size-change="changeSize">
            </Page>
            <!-- 添加管理员抽屉 -->
            <Drawer title="添加账号" :closable="false" width="40" v-model="showAdd" @on-close="drawClose">
                <Form ref="formValidate" :model="adminUserInfo" :rules="ruleInline" :label-width="100">
                    <FormItem label="部门名称" prop="username">
                        <Input v-model="adminUserInfo.name" placeholder="请输入部门名称"></Input>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="adminUserAdd()">提交</Button>
                    </FormItem>
                </Form>
            </Drawer>
        </Card>
    </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）

export default {
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: 'ID',
                    key: 'id',
                    minWidth: 50,
                    maxWidth: 100,
                },
                {
                    title: '部门',
                    key: 'name',
                    minWidth: 100,
                },
                {
                    title: '启用/禁用',
                    key: 'role_name',
                    slot: "statusSlot"
                },
                {
                    title: '操作',
                    key: 'make',
                    slot: 'memberSlot',
                    minWidth: 100,
                },
            ],
            // 数据
            list: [],
            id: '',
            total: 0,
            showAdd: false,
            // 管理信息
            adminUserInfo: {
                id: 0,
                name: ""
            },
            ruleInline: {
                name: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
            },
        }
    },
    methods: {
        search() {
            this.searchData.page = 1;
            this.getAdminUserList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getAdminUserList();
        },
        // 查询管理员信息
        getAdminUserList() {
            let params = {
                ...this.searchData,
            };
            this.$axios('/admindepartment/getList', params).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        //打开添加管理员抽屉 获取角色列表
        showAddDrawer() {
            this.showAdd = true;
        },
        // 添加管理员
        adminUserAdd() {
            this.$refs['formValidate'].validate(valid => {
                if (valid) {
                    this.$axios('/admindepartment/save', this.adminUserInfo).then(res => {
                        this.showAdd = false;
                        this.getAdminUserList();
                    });
                }
            })
        },
        drawClose() {
            this.adminUserInfo = {};
        },
        // 启用，禁用
        statusChange(id, status) {
            let statuss = status == 0 ? 1 : 0;
            this.$axios('/admindepartment/del', { id: id, status: statuss }).then(res => {
                this.getAdminUserList();
            });
        },
        // 编辑
        edit(info) {
            this.adminUserInfo.id = info.id;
            this.adminUserInfo.name = info.username;
            this.showAddDrawer()
        },
        changePage(page) {
            this.searchData.page = page;
            this.getAdminUserList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getAdminUserList();
        },
    },
    created() {
        this.getAdminUserList();
    }
}
</script>
<style></style>